package spark.mllib

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.linalg.{Matrices, Matrix, Vector, Vectors}
import org.apache.spark.mllib.stat.{KernelDensity, Statistics}
import org.apache.spark.mllib.stat.test.ChiSqTestResult
import org.apache.spark.rdd.RDD

/**
  * Created by liuwei on 2017/5/15.
  */
object ChiSquareTest {

  def main(args: Array[String]): Unit = {

    val sparkConf = new SparkConf().setAppName("ChiSquareTest").setMaster("local[8]")
    val sc = new SparkContext(sparkConf)
//    val v1: Vector = Vectors.dense(0.083019603,
//    0.808381114,
//    0.232360547,
//    0.001443696,
//    0.233725589,
//    0.660635917,
//    0.005554283,
//    0.997905135
//
//    )
//
val v1 = Vectors.dense(1279,11420,629,3,485,1051,3,536)

    val v2 = Vectors.dense(1327,11284,647,7,505,1044,2,590)

    var v3 = Vectors.dense(28,49,18,6,92,20)

    var v4= Vectors.dense(26,47,23,4,88,25)

   val v5 = Vectors.dense(30,
    110,
    86,
    23,
    5,
    5,
    4
    )
    val v6 = Vectors.dense(32,
    113,
    87,
    24,
    2,
    4,
    1
    )

    val v7 = Vectors.dense(8,4)
    val v8 = Vectors.dense(9,3)

     v4= Vectors.dense(26,47,23,4,88,25)
//val v1: Vector = Vectors.dense( 57,343,999)
//    val v2: Vector = Vectors.dense( 59,341,995)
    val p = Statistics.chiSqTest(v1,v2);
    println(p)
    val p34 = Statistics.chiSqTest(v7,v8);
    println("p34"+p34)
//      org.apache.spark.ml.stat.distribution.c\

    val seriesX: RDD[Double] =sc.parallelize(Array(1279,11420,629,3,485,1051,3,536));
    val seriesY: RDD[Double] =sc.parallelize(Array(1327,11284,647,7,505,1044,2,590));
//    val correlation: Double = Statistics.corr(seriesX, seriesY, "pearson")
//    println("correlation::::"+correlation)
//    val correlation2: Double = Statistics.corr(seriesX, seriesY, "spearman")
//    println("correlation2::::"+correlation2)
    val p1 =   Statistics.chiSqTest(v1)
    println("P1:"+p1)

    val dm: Matrix = Matrices.dense(8, 2, Array(1279,11420,629,3,485,1051,3,536,1327,11284,647,7,505,1044,2,590
    ))
    val p2 = Statistics.chiSqTest(dm);
    println("P2:"+p2)


      val p3:ChiSqTestResult = Statistics.chiSqTest(Vectors.dense(1279,11420,629,485,1051,536,1327,11284,647,505,1044,590))
      println("P3:"+p3)



//    Statistics.kolmogorovSmirnovTest()



//    val dm3: Matrix = Matrices.dense(2, 2, Array(57, 343,59,341))
//    val p3:ChiSqTestResult = Statistics.chiSqTest(dm3);
//    println(p3.statistic)
//    println(p3.degreesOfFreedom)
//    println(p3.pValue)

    val kd = new KernelDensity().setSample(seriesX).setBandwidth(3.0)

    // Find density estimates for the given values
    val densities = kd.estimate(Array(1327,11139,647,7,505,1044,2,5900))
   densities.foreach(println)

//    Statistics.kolmogorovSmirnovTest()

  }

}
